package com.turtle3d.user.visualizators;

import java.util.ArrayList;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.vecmath.Color3f;
import javax.vecmath.Matrix4f;

import com.turtle3d.ResourceManager;
import com.turtle3d.formallanguage.VisualSymbol;
import com.turtle3d.formallanguage.symbol.Symbol;
import com.turtle3d.visualizator.MyCone;
import com.turtle3d.visualizator.VisualData;
import com.turtle3d.visualizator.Visualizator;


public class Bush2 extends Visualizator {

	/**
	 * @param args
	 */
	
	public BranchGroup visualize(VisualData visualData){
		BranchGroup branchGroup = new BranchGroup();
		
		ArrayList<VisualSymbol> visualSequence = visualData.getVisualSequence();
				
		for(int i=0;i<visualSequence.size();i++){
			
			VisualSymbol visualSymbol = visualSequence.get(i);
			
			int depth = visualSymbol.getDepth();															
			Float d = visualSymbol.getSymbol().getParameter(Symbol.DISTANCE);
			float normalizedDepth = depth/(visualData.getMaximalDepthReached()*1f);
			Float r = visualSymbol.getSymbol().getParameter(Symbol.RADIUS);
			Float r_mul = visualSymbol.getSymbol().getParameter("radiusMultiplier");
			
			if(visualSymbol.getSymbol().equals(new Symbol("F"))){
				
				float r_new = r+(1-normalizedDepth)*r_mul;				
				
				Shape3D stalk = ResourceManager.getShape3D("trunkSimple");
											
				stalk.setName("Stalk");
				
				Matrix4f scale = new Matrix4f();
				scale.setIdentity();
				scale.setScale(d);
				scale.m00=r_new;
				scale.m22=r_new;
				Matrix4f transform = visualSymbol.getTransform();
				transform.mul(scale);			

				branchGroup.addChild(createTransformGroup(transform, stalk));
			}
			else if(visualSymbol.getSymbol().equals(new Symbol("B"))){
				//Shape3D leaf = ResourceManager.getShape3D("lo_leaf");
				Shape3D leaf = ResourceManager.getShape3D("face_leaf");
				
				Matrix4f scale = new Matrix4f();
				scale.setIdentity();
				
				scale.setScale(visualSymbol.getSymbol().getParameter("leafSize")*(0.2f+(float)Math.sqrt(normalizedDepth)));
				Matrix4f transform = visualSymbol.getTransform();
				transform.mul(scale);

				leaf.setName("Leafs");					
				
				branchGroup.addChild(createTransformGroup(transform, leaf));
			}
			
		}		
		
		return branchGroup;
	}
	
	public void setRequiredParameters(){
		setRequiredParamter("leafSize", 0.7f);
		setRequiredParamter("radiusMultiplier", 1.1f);
		setRequiredParamter(Symbol.RADIUS, 0.1f);
		setRequiredParamter(Symbol.VARIATION, 10f);		
	}

}
